package com.cerosoft.components;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.cerosoft.api.Config;
import com.cerosoft.utils.IndexChangeListener;

public class MusicBar extends SeekBar implements IndexChangeListener,
		OnSeekBarChangeListener {

	public void destroy() {
		if (mPlayer != null) {
			mPlayer.stop();
		}
		if (delayThread != null && delayThread.isAlive()) {
			delayThread.requestStop();
		}
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		if (fromUser && mPlayer != null) {

			int mMax = mPlayer.getDuration();
			int sMax = getMax();

			mPlayer.seekTo(mMax * progress / sMax);
		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
	}

	private MediaPlayer mPlayer = null;

	private DelayThread delayThread = null;

	private Handler mHandle = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (mPlayer == null) {
				return;
			} else {
				int position = mPlayer.getCurrentPosition();

				Log.d("test", "position=" + position);

				int mMax = mPlayer.getDuration();
				int sMax = getMax();

				setProgress(position * sMax / mMax);
			}
		}
	};

	public class DelayThread extends Thread {

		private int milliseconds;

		private boolean isStop = false;

		public void requestStop() {
			isStop = true;
		}

		public DelayThread(int i) {
			milliseconds = i;
		}

		public void run() {
			while (!isStop) {
				try {
					sleep(milliseconds);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				mHandle.sendEmptyMessage(0);
			}
		}
	}

	private List<String> audios = null;

	private String ipAddr = null;

	private int currIndex = -1;

	private void getAudioByIndex() {
		if (audios == null) {
			return;
		}
		try {
			String currPath = audios.get(currIndex);
			if (mPlayer != null && mPlayer.isPlaying()) {
				mPlayer.stop();
			}

			mPlayer = new MediaPlayer();

			String dataSourceStr = ipAddr + Config.DOWNLOAD_URL_PART
					+ URLEncoder.encode(currPath);

			// String dataSourceStr = ipAddr + Config.DOWNLOAD_URL_PART +
			// currPath;

			Log.d("m", "getAudioByIndex" + "path=" + currPath);

			Log.d("m", "getAudioByIndex" + "dataSourceStr=" + dataSourceStr);
			mPlayer.setDataSource(dataSourceStr);
			if (delayThread != null && !delayThread.isAlive()) {
				delayThread.start();
			}
			mPlayer.prepare();
			mPlayer.start();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void initTitleBar(List<String> audios, String ipAddr, int currIndex) {
		this.audios = audios;
		this.ipAddr = ipAddr;
		this.currIndex = currIndex;
		getAudioByIndex();
	}

	public MusicBar(Context context, AttributeSet attrs) {
		super(context, attrs);

		// mPlayer = new MediaPlayer();
		delayThread = new DelayThread(100);

		setOnSeekBarChangeListener(this);
	}

	@Override
	public int indexAdd() {
		if (audios != null && currIndex < audios.size() - 1) {
			currIndex++;
			getAudioByIndex();
		}
		return 0;
	}

	@Override
	public int indexSub() {
		if (audios != null && currIndex > 0) {
			currIndex--;
			getAudioByIndex();
		}
		return 0;
	}

}
